package com.common.exception;

import com.common.ErrorCodeEnum;
import com.common.utils.file.fileException.FileException;
import com.common.utils.Utils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.ui.Model;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName UnionExceptionHandler
 * @Data 2019/11/22 11:44
 * @Auther zql
 * 全局异常捕获
 **/
@Slf4j
@RestControllerAdvice
public class UnionExceptionHandler {
    /**
     * 应用到所有@RequestMapping注解方法，在其执行之前初始化数据绑定器
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
//        log.debug("binder.getFieldDefaultPrefix {}", binder.getFieldDefaultPrefix());
//        log.debug("binder.getFieldMarkerPrefix {}", binder.getFieldMarkerPrefix());
    }

    /**
     * 把值绑定到Model中，使全局@RequestMapping可以获取到该值
     *
     * @param model
     */
    @ModelAttribute
    public void addAttributes(Model model) {
        model.addAttribute("author", "harry");
    }

    /**
     * Description : 全局异常捕捉处理
     * Group :
     *
     * @param ex
     * @return
     * @author honghh
     * @date 2019/4/1 0001 10:34
     */
    @ExceptionHandler(RRException.class)
    public JSONObject apiExceptionHandler(RRException ex) {
        log.error("ApiException 异常抛出：{}", ex);
        JSONObject json = new JSONObject();
        json.put("yes", "异常捕获成功");
        return json;
    }

    //业务代码异常
    @ExceptionHandler(BusinessException.class)
    public JSONObject apiExceptionHandler2(BusinessException ex) {
        log.error("ApiException 异常抛出：{}", ex);
        return Utils.result(ex.getErrorCodeEnum());
    }

    //参数错误异常捕获
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JSONObject apiExceptionHandler3(MethodArgumentNotValidException exception) {
        ErrorCodeEnum errorCodeEnum = ErrorCodeEnum.PARAM_FAILED;
        errorCodeEnum.setErrorMsg(exception.getBindingResult().getFieldError().getDefaultMessage());
        return Utils.result(errorCodeEnum);
    }

    //JSON反序列化错误异常捕获
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public JSONObject apiExceptionHandler2(HttpMessageNotReadableException exception) {
        ErrorCodeEnum errorCodeEnum = ErrorCodeEnum.JSON_PARSE_FAILED;
        errorCodeEnum.setErrorMsg(exception.getMessage());
        return Utils.result(errorCodeEnum);
    }

    @ExceptionHandler(UtilException.class)
    public JSONObject utilExceptionHandler(UtilException exception) {
        ErrorCodeEnum errorCodeEnum = ErrorCodeEnum.UTIL_FAILED;
        errorCodeEnum.setErrorMsg(exception.getMessage());
        return Utils.result(errorCodeEnum);
    }

    @ExceptionHandler(FileException.class)
    public JSONObject fileExceptionHandler(UtilException exception) {
        ErrorCodeEnum errorCodeEnum = ErrorCodeEnum.FILE_FAILED;
        errorCodeEnum.setErrorMsg(exception.getMessage());
        return Utils.result(errorCodeEnum);
    }
}

